package com.qlm.hbutil;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.kit.PaymentKit;
import com.jfinal.weixin.sdk.utils.HttpUtils;

public class HbUtil {
	
	/** 
	 * post
	 * 微信红包发送接口
	 * */
	private static final String weixin_hb = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
	
	private static final String weixin_groupredpack = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";
	
	private static final String weixin_transfers = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
	
	/**
	 * 企业付款
	 * @param params mch_appid appid
	 * @param params openid 用户openid
	 * @param params amount 金额
	 * @param paternerKey Key
	 * @param p12证书路径
	 * */
	public static Map<String, String> transfersRequest(String openId,int money,String certPath,String orderNO,String act_name) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("openid", openId);
		params.put("amount",money+"");
		params.put("mchid", PropKit.get("mch_id"));//商户号
		params.put("partner_trade_no", orderNO);//商户订单号
		params.put("check_name", "NO_CHECK");//校验用户姓名选项
		params.put("desc", act_name);//企业付款描述信息
		params.put("mch_appid", PropKit.get("appid"));
		params.put("spbill_create_ip", "127.0.0.1");//Ip地址
		params.put("nonce_str", System.currentTimeMillis()+"");//随机字符串
		//String sign = WxUtil.createSign(params, PropKit.get("partner_key"));
		String sign = PaymentKit.createSign(params, PropKit.get("partner_key"));
		params.put("sign", sign);
		String xmlStr = HttpUtils.postSSL(weixin_transfers, PaymentKit.toXml(params),certPath,PropKit.get("mch_id"));
		return PaymentKit.xmlToMap(xmlStr);
	}
	
	/**
	 * 现金开发红包接口
	 * @param params re_openid 用户openid
	 * @param params total_amount 金额
	 * @param params act_name 活动名称
	 * @param params wishing 红包祝福语
	 * @param params remark 备注
	 * @param number 发送总人数
	 * */
	public static Map<String, String> groupredPackRequest(Map<String, String> params, String paternerKey,String certPath
			,String hbType,String number) {
		params.put("nonce_str",  System.currentTimeMillis()+"");
//		params.put("mch_billno", WxUtil.getOrderNo(PropKit.get("paterner")));//商户订单号
		params.put("mch_id", PropKit.get("mch_id"));
		params.put("wxappid", PropKit.get("appid"));//appid
		params.put("send_name", PropKit.get("sendname"));//商户名称
		params.put("total_num", number);
		params.put("client_ip", "127.0.0.1");
		String sign =PaymentKit.createSign(params, paternerKey);
//		sign = WxUtil.createSign(params, paternerKey);
		
		
		params.put("sign", sign);
		String xmlStr = "";
		if (hbType.equals("oneHb")) {			
			xmlStr = HttpUtils.postSSL(weixin_hb, PaymentKit.toXml(params),certPath,PropKit.get("mch_id"));			
		}else{
			params.put("amt_type", "ALL_RAND");
			xmlStr = HttpUtils.postSSL(weixin_groupredpack, PaymentKit.toXml(params),certPath,PropKit.get("mch_id"));
		}
		
		return PaymentKit.xmlToMap(xmlStr);
	}
	
}

